//引用gulp模块
const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');
const fileinclude = require('gulp-file-include');
const less = require('gulp-less');
const csso = require('gulp-csso');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
//建立任务
//参数：1.任务名称 2.任务的回调函数
gulp.task('first', () => {
    console.log('11');
    //1.用gulp.src获取要处理的文件
    return gulp.src('./src/css/base.css')
        .pipe(gulp.dest('dist/css'));
});


//html任务
//1.压缩
//2.提取公共部分
gulp.task('htmlmin', () => {
    return gulp.src('./src/*.html')
        .pipe(fileinclude())
        //压缩html中的代码
        .pipe(htmlmin({
            collapseWhitespace: true
        }))
        .pipe(gulp.dest('dist'));
});

//css任务
//1.less语法转换
//2.css代码压缩
gulp.task('cssmin', () => {
    return gulp.src(['./src/css/*.less', './src/css/*.css'])
        .pipe(less())
        .pipe(csso())
        .pipe(gulp.dest('dist/css'))
});

//js任务
//1.es6代码转换
//2.代码压缩
gulp.task('jsmin', () => {
    return gulp.src('./src/js/*.js')
        .pipe(babel({
            //转换
            presets: ['@babel/env']
        }))
        //压缩
        .pipe(uglify())
        .pipe(gulp.dest('dist/js'))
});
//复制文件夹
gulp.task('copy', () => {
    return gulp.src('./src/images/*')
        .pipe(gulp.dest('dist/images'));
    gulp.src('./src/lib/*')
        .pipe(gulp.dest('dist/lib'))
});
gulp.task('copyy', () => {
    return gulp.src('./src/lib/*')
        .pipe(gulp.dest('dist/lib'))
});

//构建任务
gulp.task('default', gulp.series(['htmlmin', 'cssmin', 'jsmin', 'copy', 'copyy',
    async (done) => done
]))